using System;
using System.Collections.Generic;
using System.Text;

namespace Edge.Common.Web
{
    public class CheckBaseAndCurrentUrl
    {
        private UrlBuilder currentUrl = null;
        private UrlBuilder baseUrl = null;

        public CheckBaseAndCurrentUrl()
        {
            string baseUrlApplication = System.Configuration.ConfigurationManager.AppSettings["BaseUrlApplication"];
            currentUrl = UrlBuilder.FromCurrentRequest();
            if (baseUrlApplication  != null && baseUrlApplication != string.Empty)
            {
                if (!baseUrlApplication.EndsWith("/"))
                    baseUrlApplication += "/";
                baseUrl = new UrlBuilder(baseUrlApplication);
            }
        }

        public bool NeedRedirect(out string urlRedirect)
        {
            urlRedirect = string.Empty;
            if (baseUrl != null &&
                (currentUrl.Scheme != baseUrl.Scheme ||
                currentUrl.Host != baseUrl.Host ||
                currentUrl.Port != baseUrl.Port))
            {
                urlRedirect = baseUrl.Uri.AbsoluteUri + currentUrl.PageName +
                              (currentUrl.Uri.Query != string.Empty ? currentUrl.Uri.Query : "");
                return true;
            }
            return false;
        }

    }
}
